package org.dizem.groupon;

import org.apache.log4j.Logger;
import org.dizem.groupon.dao.*;
import org.dizem.groupon.model.domain.*;
import org.dizem.util.MD5Util;
import org.dizem.util.SpringUtil;
import org.dizem.util.XmlReader;
import org.hibernate.Hibernate;
import org.w3c.dom.Node;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;

/**
 * User: dizem@126.com
 * Time: 11-6-23 下午1:10
 */
public class MockData {
	private static Logger log = Logger.getLogger(MockData.class);

	private XmlReader reader;

	public MockData() {
		reader = new XmlReader("data/data.mock.xml");
		addCategory();
		addCity();
		addUser();
		addLink();

		try {
			addItem();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		addReply();
	}

	public static void main(String[] args) {
		new MockData();
	}


	public void addUser() {
		UserDAO userDAO = (UserDAO) SpringUtil.getBean("userDAO");
		CityDAO cityDAO = (CityDAO) SpringUtil.getBean("cityDAO");
		for (Node node : reader.getNodeList("users")) {
			UserEntity user = new UserEntity();
			user.setUsername(XmlReader.getAttribute(node, "username"));
			user.setCity(cityDAO.getByChnName(XmlReader.getAttribute(node, "city")));
			user.setPassword(MD5Util.encode(XmlReader.getAttribute(node, "password")));
			user.setMobile(XmlReader.getAttribute(node, "mobile"));
			user.setEmail(XmlReader.getAttribute(node, "email"));
			user.setRegisterTime(new Date());
			user.setAccountType(Integer.parseInt(XmlReader.getAttribute(node, "accountType")));
			userDAO.save(user);

			log.info("save user:" + user.getUsername() + "/" + user.getEmail());
		}
	}

	public void addCategory() {
		CategoryDAO dao = (CategoryDAO) SpringUtil.getBean("categoryDAO");
		for (Node node : reader.getNodeList("categories")) {
			CategoryEntity category = new CategoryEntity();
			category.setName(XmlReader.getAttribute(node, "name"));
			dao.save(category);


			log.info("save " + category);
		}
	}

	public void addCity() {
		CityDAO cityDAO = (CityDAO) SpringUtil.getBean("cityDAO");
		for (Node node : reader.getNodeList("cities")) {
			CityEntity city = new CityEntity();
			city.setChnName(XmlReader.getAttribute(node, "chn"));
			city.setEngName(XmlReader.getAttribute(node, "eng"));
			cityDAO.save(city);


			log.info("save " + city);
		}
	}


	public void addItem() throws ParseException, IOException {
		ItemDAO itemDAO = (ItemDAO) SpringUtil.getBean("itemDAO");
		CityDAO cityDAO = (CityDAO) SpringUtil.getBean("cityDAO");
		CategoryDAO categoryDAO = (CategoryDAO) SpringUtil.getBean("categoryDAO");
		MerchantDAO merchantDAO = (MerchantDAO) SpringUtil.getBean("merchantDAO");
		ImageDAO imageDAO = (ImageDAO) SpringUtil.getBean("imageDAO");

		for (Node node : reader.getNodeList("items")) {

			MerchantEntity merchant = new MerchantEntity();
			Node merchantNode = XmlReader.getChildNode(node, "merchant");
			merchant.setPassword(MD5Util.encode("123456"));
			merchant.setName(XmlReader.getChildNodeValue(merchantNode, "name").trim());
			merchant.setAddress(XmlReader.getChildNodeValue(merchantNode, "address").trim());
			merchant.setUrl(XmlReader.getChildNodeValue(merchantNode, "url").trim());
			merchant.setPhoneNumber(XmlReader.getChildNodeValue(merchantNode, "phone").trim());
			merchantDAO.save(merchant);


			ItemEntity item = new ItemEntity();
			item.setMerchant(merchant);
			item.setName(XmlReader.getChildNodeValue(node, "name").trim());
			item.setCity(cityDAO.getByChnName(XmlReader.getChildNodeValue(node, "city").trim()));
			item.setCategory(categoryDAO.getByName(XmlReader.getChildNodeValue(node, "category").trim()));
			item.setTitle(XmlReader.getChildNodeValue(node, "title").trim());
			item.setFeature(XmlReader.getChildNodeValue(node, "feature").trim());
			item.setTips(XmlReader.getChildNodeValue(node, "tips").trim());
			item.setDescription(XmlReader.getChildNodeValue(node, "description").trim());
			item.setSpecialTips(XmlReader.getChildNodeValue(node, "specialTips").trim());
			item.setGroupPrice(Float.parseFloat(XmlReader.getChildNodeValue(node, "groupPrice").trim()));
			item.setMarketPrice(Float.parseFloat(XmlReader.getChildNodeValue(node, "marketPrice").trim()));
			item.setMinCount(Integer.parseInt(XmlReader.getChildNodeValue(node, "minCount").trim()));
			item.setMaxCount(Integer.parseInt(XmlReader.getChildNodeValue(node, "maxCount").trim()));
			item.setBeginTime(Constants.DATE_FORMAT.parse(XmlReader.getChildNodeValue(node, "beginTime").trim()));
			item.setEndTime(Constants.DATE_FORMAT.parse(XmlReader.getChildNodeValue(node, "endTime").trim()));
			String imgPrefix = XmlReader.getChildNodeValue(node, "image").trim();
			for (int i = 0; i < 3; ++i) {
				ImageEntity image = new ImageEntity();
				image.setImage(Hibernate.createBlob(new FileInputStream(new File("data/" + imgPrefix + i + ".jpg"))));
				imageDAO.save(image);
				switch (i) {
					case 0:
						item.setImage1(image);
						break;
					case 1:
						item.setImage2(image);
						break;
					case 2:
						item.setImage3(image);
						break;
				}
			}
			itemDAO.save(item);

			log.info("save item: " + item.getName());
		}
	}

	private void addLink() {
		LinkDAO linkDAO = (LinkDAO) SpringUtil.getBean("linkDAO");
		for (Node node : reader.getNodeList("links")) {
			LinkEntity link = new LinkEntity();
			link.setName(XmlReader.getAttribute(node, "name"));
			link.setUrl(XmlReader.getAttribute(node, "url"));
			linkDAO.save(link);
			log.info("save " + link);
		}
	}

	private void addReply() {
		ReplyDAO replyDAO = (ReplyDAO) SpringUtil.getBean("replyDAO");
		UserDAO userDAO = (UserDAO) SpringUtil.getBean("userDAO");

		ReplyEntity reply = new ReplyEntity();
		reply.setItemId(2);
		reply.setMessage("很好很强大");
		reply.setTime(new Date());
		reply.setUser(userDAO.getByUsername("dizem"));


		replyDAO.save(reply);
	}
}
